//
//  SPMessageCell.m
//  Sport
//
//  Created by Cass on 2018/4/7.
//  Copyright © 2018年 Cass. All rights reserved.
//

#import "SPMessageCell.h"
#import "SPMessage.h"
#import "SPMessageFrame.h"
@interface SPMessageCell()
@property (nonatomic, weak) UILabel *timeLabel;
@property (nonatomic, weak) UIImageView *iconImgView;
@property (nonatomic, weak) UIButton *btnText;
@end
@implementation SPMessageCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
}

//重写initWithStyle方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        //设置时间Label
        UILabel *timeLabel = [[UILabel alloc]init];
        timeLabel.font = [UIFont systemFontOfSize:12.0f];
        timeLabel.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:timeLabel];
        self.timeLabel = timeLabel;
        
        //设置头像ImageView
        UIImageView *iconImgView = [[UIImageView alloc]init];
        [self.contentView addSubview:iconImgView];
        self.iconImgView = iconImgView;
        
        //设置消息文本labelText
        UIButton *btntext = [[UIButton alloc]init];
        btntext.titleLabel.numberOfLines = 0;
        btntext.titleLabel.font = [UIFont systemFontOfSize:13.0f];
        btntext.contentEdgeInsets = UIEdgeInsetsMake(15, 20, 15, 10);
        [self.contentView addSubview:btntext];
        self.btnText = btntext;
    }
    self.backgroundColor = [UIColor clearColor];
    return self;
}

- (void)setMessageFrame:(SPMessageFrame *)messageFrame{
    _messageFrame = messageFrame;
    //获取数据模型
    SPMessage *message= messageFrame.message;
    //设置时间label
    self.timeLabel.text = message.time;
    self.timeLabel.frame = messageFrame.timeFrame;
    self.timeLabel.hidden = message.hideTime;
    //设置头像
    NSString *iconImg = message.type == SPMessageTypeMe ? @"me" : @"other";
    self.iconImgView.image = [UIImage imageNamed:iconImg];
    self.iconImgView.frame = messageFrame.iconFrame;
    self.iconImgView.layer.cornerRadius = self.iconImgView.bounds.size.width / 2;
    self.iconImgView.layer.masksToBounds = YES;
    //设置正文
    [self.btnText setTitle:message.text forState:UIControlStateNormal];
    self.btnText.frame = messageFrame.messageFrame;
    //设置气泡图
    NSString *imgNor, *imgHighlighted;
    if (message.type == SPMessageTypeMe) {
        //        self.backView.image = [UIImage imageNamed:@"chat_send_nor"];
        imgNor = @"chat_send_nor";
        imgHighlighted = @"chat_send_press_pic";
        [self.btnText setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }else{
        //        self.backView.image = [UIImage imageNamed:@"chat_recive_nor"];
        imgNor = @"chat_recive_nor";
        imgHighlighted = @"chat_recive_press_pic";
        [self.btnText setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    
    UIImage *imageNormal = [UIImage imageNamed:imgNor];
    UIImage *imageHighlighted = [UIImage imageNamed:imgHighlighted];
    // 拉伸图片 参数1 代表从左侧到指定像素禁止拉伸，该像素之后拉伸，参数2 代表从上面到指定像素禁止拉伸，该像素以下就拉伸
    
    imageNormal = [imageNormal stretchableImageWithLeftCapWidth:imageNormal.size.width / 2 topCapHeight:imageNormal.size.height / 2];
    imageHighlighted = [imageHighlighted stretchableImageWithLeftCapWidth:imageHighlighted.size.width / 2 topCapHeight:imageHighlighted.size.height / 2];
    //设置气泡
    [self.btnText setBackgroundImage:imageNormal forState:UIControlStateNormal];
    [self.btnText setBackgroundImage:imageHighlighted forState:UIControlStateHighlighted];
}
+ (instancetype)messageCellWithTableView:(UITableView *)tableView{
    static NSString *ID = @"cell";
    SPMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[SPMessageCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    
}

@end
